/*
** exec_cmd.c for  in /home/gomes_m//my-project-a-moi/server
** 
** Made by mickael gomes
** Login   <gomes_m@epitech.net>
** 
** Started on  Fri Jul 26 13:07:16 2013 mickael gomes
** Last update Wed Jul 31 00:01:32 2013 mickael gomes
*/

#include		<stdio.h>
#include		<unistd.h>
#include		<stdlib.h>
#include		<sys/types.h>
#include		<sys/wait.h>
#include		"struct.h"
#include		"prototypes.h"

void			do_cmd(t_cmd *cmd)
{
  int			i = 0;
  int			bool = 1;
  char			*path;

  while (cmd->paths[i] && bool)
    {
      path = my_strcat_plus(cmd->paths[i], cmd->cmd, '/');
      if ((bool = access(path, X_OK | F_OK)) != 0)
	i++;
    }
  if (i == tab_len(cmd->paths))
    my_putstr(2, "Command not found\n", 0);
  else
    {
      path = my_strcat_plus(cmd->paths[i], cmd->cmd, '/');
      if (execve(path, cmd->cmd_tab, cmd->env) == -1)
	my_putstr(2, "Exec error.\n", 0);
    }
}

void			get_fork(t_cmd *cmd, t_socket *socket)
{
  pid_t			son_pid;
  int			status;

  (void)socket;
  son_pid = fork();
  wait(&status);
  if (son_pid == 0)
    do_cmd(cmd);
}
